//: exceptions/LoggingExceptions.java
// An exception that reports through a Logger.
package exceptions;
import java.util.logging.*;
import java.io.*;

class LoggingException extends Exception {
  private static Logger logger = Logger.getLogger("LoggingException");
  public LoggingException() {
    StringWriter trace = new StringWriter();//创建字符串打印流
    printStackTrace(new PrintWriter(trace));//将调用栈信息打印到打印流
    logger.severe(trace.toString());//Logger记录日志信息，trace记录的就是调用栈的信息
  }
}

public class LoggingExceptions {
  public static void main(String[] args) {
    try {
      throw new LoggingException();
    } catch(LoggingException e) {
      System.err.println("Caught " + e);
    }
    try {
      throw new LoggingException();
    } catch(LoggingException e) {
      System.err.println("Caught " + e);
    }
  }
} /* Output: (85% match)
Aug 30, 2005 4:02:31 PM LoggingException <init>
SEVERE: LoggingException
        at LoggingExceptions.main(LoggingExceptions.java:19)

Caught LoggingException
Aug 30, 2005 4:02:31 PM LoggingException <init>
SEVERE: LoggingException
        at LoggingExceptions.main(LoggingExceptions.java:24)

Caught LoggingException
*///:~
